//
//  LDY_ArticleCollectionViewController.m
//  ProjectOfBStage
//
//  Created by dlios on 15/11/19.
//  Copyright © 2015年 cuixin. All rights reserved.
//

#import "LDY_ArticleCollectionViewController.h"
#import "LDY_ReadDetailTableViewCell.h"
#import "LDY_DataBaseManager.h"
#import <SDImageCache.h>
#import <UIImageView+WebCache.h>
#import "LDY_ReadContectViewController.h"

@interface LDY_ArticleCollectionViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) LDY_DataBaseManager *dataBase;

@property (nonatomic, retain) NSMutableArray *mainArr;//数据源

@property (nonatomic, retain) NSArray *tempArr;

@property (nonatomic, retain) NSMutableArray *titleArr;

@property (nonatomic, retain) NSMutableArray *imgArr;

@property (nonatomic, retain) NSMutableArray *contentArr;

@property (nonatomic, retain) NSMutableArray *idArr;

@end

@implementation LDY_ArticleCollectionViewController

#pragma mark - 视图将要出现
- (void)viewWillAppear:(BOOL)animated
{
    
    self.tabBarController.tabBar.hidden = YES;
    
    self.navigationController.navigationBarHidden = YES;
    
    [self getData];
    
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.mainArr = [NSMutableArray array];
        self.tempArr = [NSArray array];
        self.titleArr = [NSMutableArray array];
        self.imgArr = [NSMutableArray array];
        self.contentArr = [NSMutableArray array];
        self.idArr = [NSMutableArray array];
    }
    return self;
}                 

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.barView.backgroundColor = [UIColor blackColor];

    /* 创建返回按钮 */
    [self.barViewLeftButton setImage:[UIImage imageNamed:@"gobackbtn@2x"] forState:UIControlStateNormal];
    [self.barViewLeftButton addTarget:self action:@selector(barViewLeftButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [self createTableView];
}

/* 返回事件 */
- (void)barViewLeftButtonClicked
{
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)getData
{
    // 创建数据库
    self.dataBase = [LDY_DataBaseManager shareDataBaseManager];
    // 连接数据库
    [self.dataBase connectDataBase:@"DataBase.sqlite"];
    
    // 获取
    self.mainArr = [NSMutableArray array];
    self.tempArr = [self.dataBase execuateQuerySQL:@"select * from LDYSaveTable"];
    self.mainArr = [NSMutableArray arrayWithArray:self.tempArr];
    
    if (self.mainArr.count == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示:" message:@"还没收藏任何东西" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alert show];
        [alert release];
    }
    self.titleArr = [NSMutableArray array];
    self.imgArr = [NSMutableArray array];
    self.contentArr = [NSMutableArray array];
    self.idArr = [NSMutableArray array];
    
    for (NSDictionary *dic in self.mainArr) {
        
        [self.titleArr addObject:[dic objectForKey:@"l_title"]];
        
        [self.imgArr addObject:[dic objectForKey:@"l_img"]];
        
        [self.contentArr addObject:[dic objectForKey:@"l_content"]];
        
        [self.idArr addObject:[dic objectForKey:@"l_id"]];
    }
    [self.tableView reloadData];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 45 * KHEIGHT, KWIDTH * 375, KHEIGHT * 667 - KHEIGHT * 45) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.rowHeight = 150 * KHEIGHT;
    
    [self.tableView release];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LDY_ReadContectViewController *readContect = [[LDY_ReadContectViewController alloc] init];
    
    readContect.ID = [self.idArr objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:readContect animated:YES];
    
    [readContect release];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.imgArr.count > 0) {
        return self.imgArr.count;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.imgArr.count > 0) {
        
        LDY_ReadDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LDYAaveCell"];
        if (!cell) {
            cell = [[LDY_ReadDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LDYAaveCell"];
        }
        
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:[self.imgArr objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"wait"]];
        
        cell.titleLabel.text = [self.titleArr objectAtIndex:indexPath.row];
        
        cell.contentLabel.text = [self.contentArr objectAtIndex:indexPath.row];
        
        return cell;
        
    }else{
        LDY_ReadDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LDYAaveCell2"];
        if (!cell) {
            cell = [[LDY_ReadDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LDYAaveCell2"];
        }
        cell.backgroundColor = [UIColor clearColor];
        
        return cell;
    }
}

#pragma mark - cell的编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

#pragma mark - 删除<我的收藏>中的数据
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 如果是删除类型
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 创建数据库对象
        LDY_DataBaseManager *dataManager = [LDY_DataBaseManager shareDataBaseManager];
        // 连接数据库
        [dataManager connectDataBase:@"DataBase.sqlite"];
        // 删除数据库中的数据
        [dataManager execuateQuerySQL:[NSString stringWithFormat:@"delete from LDYSaveTable where l_id = '%@'", [self.mainArr[indexPath.row] objectForKey:@"l_id"]]];
        
        [self.mainArr removeObjectAtIndex:indexPath.row];
        // 数据源删除响应位置的数据
        [self.titleArr removeObjectAtIndex:indexPath.row];
        
        [self.imgArr removeObjectAtIndex:indexPath.row];
        
        [self.contentArr removeObjectAtIndex:indexPath.row];
        
        [self.idArr removeObjectAtIndex:indexPath.row];
        
        // 把删除的位置信息存到数组里
        NSArray *array = @[indexPath];
        // 让数据和视图同步, 动画效果
        [self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationLeft];
    }
}

// alertView的点击(提示你没有收藏任何东西, 返回上一个界面)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
